﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using RM.WinClient.Utilities;

namespace RM.WinClient
{
    public partial class MasterPage
    {
        protected System.Windows.Forms.NotifyIcon TrayNotify;
        private System.Windows.Forms.ContextMenuStrip ctxTrayMenu;
        private System.Windows.Forms.ToolStripMenuItem mnuExit;
        protected System.Windows.Forms.ToolStripMenuItem mnuStop;
        protected System.Windows.Forms.ToolStripMenuItem mnuCandidateList;
        protected bool isAppExiting = false;

        public void InitializeTray()
        {
            if (App.Current.Resources["NotifyIcon"] == null)
                App.Current.Resources["NotifyIcon"] = new System.Windows.Forms.NotifyIcon();

            TrayNotify = App.Current.Resources["NotifyIcon"] as System.Windows.Forms.NotifyIcon;

            // This icon file needs to be in the bin folder of the application
            TrayNotify.Icon = new System.Drawing.Icon("../../Application.ico"); 

            //show the Tray Notify Icon
            TrayNotify.Visible = true;
            Closing += new System.ComponentModel.CancelEventHandler(TrayWindow_Closing);
            //Create a object for the context menu
            ctxTrayMenu = new System.Windows.Forms.ContextMenuStrip();

            //Add the Menu Item to the context menu
            mnuStop = new System.Windows.Forms.ToolStripMenuItem();
            mnuStop.Visible = false;
            mnuStop.Text = "Stop";
            mnuStop.Click += new EventHandler(mnuStop_Click);
            ctxTrayMenu.Items.Add(mnuStop);

            //Add the Menu Item to the context menu
            mnuCandidateList = new System.Windows.Forms.ToolStripMenuItem();
            mnuCandidateList.Visible = false;
            mnuCandidateList.Text = "Candidate List";
            mnuCandidateList.Click += new EventHandler(mnuCandidateList_Click);
            ctxTrayMenu.Items.Add(mnuCandidateList);
 
            mnuExit = new System.Windows.Forms.ToolStripMenuItem();
            mnuExit.Text = "Exit";
            mnuExit.Click += new EventHandler(mnuExit_Click);
            ctxTrayMenu.Items.Add(mnuExit);

            //mnuCandidateList = new System.Windows.Forms.ToolStripMenuItem();
            //mnuCandidateList.Text = "Candidate List";
            //mnuCandidateList.Click += new EventHandler(mnuCandidateList_Click);
            //ctxTrayMenu.Items.Add(mnuCandidateList); 

            //Add the Context menu to the Notify Icon Object
            TrayNotify.ContextMenuStrip = ctxTrayMenu;

            TrayNotify.MouseMove += new System.Windows.Forms.MouseEventHandler(TrayNotify_MouseMove);

        }

        void TrayWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            //When the application is closed, check wether the application is 
            //exiting from menu or forms close button
            if (!isAppExiting)
            {
                //if the forms close button is triggered, cancel the event and hide the form
                //then show the notification ballon tip
                e.Cancel = true;
                this.Hide();
                TrayNotify.BalloonTipIcon = System.Windows.Forms.ToolTipIcon.Info;
                TrayNotify.BalloonTipTitle = "Tray Notify";
                TrayNotify.BalloonTipText = "Tray Notify has been minimized to the system tray. To open the application, double-click the icon in the system tray.";
                TrayNotify.ShowBalloonTip(400);
            }
        }

        void mnuExit_Click(object sender, EventArgs e)
        {
            isAppExiting = true;
            this.Close();
            TrayNotify.Visible = false;
            
        }

        protected virtual void mnuCandidateList_Click(object sender, EventArgs e)
        {
            this.Show();
        }

        protected virtual void mnuStop_Click(object sender, EventArgs e)
        {
            if (MessageBoxResult.Yes == MessageBox.Show("Are you sure you want to stop the test?", "Stop", MessageBoxButton.YesNo))
            {
                TestTimer.Stop();
            }
        }

        #region Show Tool Tip In Tray
        void TrayNotify_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            TrayNotify.Text = Util.FormatTime(TestTimer.RunningTime);
        }
        #endregion

    }
}
